import { mapGetters } from "vuex";
export default {
  computed: {
    ...mapGetters("cart", ["total"]),
  },
  watch: {
    total: {
      immediate: true,
      handler(newValue) {
        // ------------------------------
        const tabBarPagePath = ['pages/home/home', 'pages/category/category', 'pages/cart/cart', 'pages/my/my']
        const pageStack = getCurrentPages()
        const currentPage = pageStack[pageStack.length - 1]
        const currentRoute = currentPage.route
        if (!tabBarPagePath.includes(currentRoute)) return
        // ----------------------------

        if (newValue > 0) {
          // 设置角标
          uni.setTabBarBadge({
            index: 2,
            text: "" + newValue,
          });
        } else {
          // 移除角标
          uni.removeTabBarBadge({
            index: 2,
          });
        }
      },
    },
  },
  // 只要页面进入前台就会触发
  onShow() {
    if (this.total > 0) {
      // 设置角标
      uni.setTabBarBadge({
        index: 2,
        text: "" + this.total,
      });
    } else {
      // 移除角标
      uni.removeTabBarBadge({
        index: 2,
      });
    }
  }
}